﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using MLM.AdminCP.Class;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace MLM.AdminCP
{
    public static class Email
    {
        public static string Send_Email(string SendTo, string Subject, string Body)
        {
            try
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                bool result = regex.IsMatch(SendTo);
                if (result == false)
                {
                    return "Email wrong!";
                }
                else
                {
                    using (var DataContext = new Data.MLMDataContext())
                    {
                        System.Net.Mail.SmtpClient smtp = new SmtpClient();
                        System.Net.Mail.MailMessage msg = new MailMessage(CommonSettingHelper.GetCommonSetting(DataContext, CommonSettingsContstants.EMAIL_FROMMAIL), SendTo, Subject, Body);
                        msg.IsBodyHtml = true;
                        var host = CommonSettingHelper.GetCommonSetting(DataContext, CommonSettingsContstants.EMAIL_SMTPHOST);
                        var port = CommonSettingHelper.GetCommonSetting(DataContext, CommonSettingsContstants.EMAIL_PORT);
                        var Email = CommonSettingHelper.GetCommonSetting(DataContext, CommonSettingsContstants.EMAIL_USERNAME);
                        var pass = CommonSettingHelper.GetCommonSetting(DataContext, CommonSettingsContstants.EMAIL_PASSWORD);
                        smtp.Host = host;// "smtp.gmail.com";//Sử dụng SMTP của gmail
                        smtp.Credentials = new System.Net.NetworkCredential(Email, pass);
                        smtp.Port = int.Parse(port);
                        smtp.EnableSsl = true;
                        ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                        smtp.Send(msg);
                        return "An email sent to: " + SendTo + " !.";
                    }
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public static string Send_Email_With_Attachment(string SendTo, string SendFrom, string Subject, string Body, string AttachmentPath)
        {
            try
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                string from = SendFrom;
                string to = SendTo;
                string subject = Subject;
                string body = Body;
                bool result = regex.IsMatch(to);
                if (result == false)
                {
                    return "Địa chỉ email không hợp lệ.";
                }
                else
                {
                    try
                    {
                        MailMessage em = new MailMessage(from, to, subject, body);
                        Attachment attach = new Attachment(AttachmentPath);
                        em.Attachments.Add(attach);
                        em.Bcc.Add(from);
                        System.Net.Mail.SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";//Ví dụ xử dụng SMTP của gmail                   
                        smtp.Send(em);
                        return "";
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public static string Send_Email_With_BCC_Attachment(string SendTo, string SendBCC, string SendFrom, string Subject, string Body, string AttachmentPath)
        {
            try
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                string from = SendFrom;
                string to = SendTo; //Danh sách email được ngăn cách nhau bởi dấu ";"
                string subject = Subject;
                string body = Body;
                string bcc = SendBCC;
                bool result = true;
                String[] ALL_EMAILS = to.Split(';');
                foreach (string emailaddress in ALL_EMAILS)
                {
                    result = regex.IsMatch(emailaddress);
                    if (result == false)
                    {
                        return "Địa chỉ email không hợp lệ.";
                    }
                }
                if (result == true)
                {
                    try
                    {
                        MailMessage em = new MailMessage(from, to, subject, body);
                        Attachment attach = new Attachment(AttachmentPath);
                        em.Attachments.Add(attach);
                        em.Bcc.Add(bcc);

                        System.Net.Mail.SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";//Ví dụ xử dụng SMTP của gmail
                        smtp.Send(em);

                        return "";
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}